//
//  YQRefundOrderListVC.m
//  MuYing
//
//  Created by tbb on 2020/3/5.
//  Copyright © 2020 TBB. All rights reserved.
//售后订单列表

#import "YQRefundOrderListVC.h"
#import "YQGoodsOrderDetailsVC.h"
#import "YQOrderLogisticsWriteVC.h"
#import "YQRefundOrderDetailsVC.h"

#import "YQGoodsActionOrderDetailsVC.h"
#import "YQGoodsScenicspotOrderDetailsVC.h"
#import "YQGoodsHotelOrderDetailsVC.h"

#import "YQPOrderLogicsViewController.h"

#import "YQRefundOrderModel.h"

#import "YQRefundGoodInfoCell.h"

@interface YQRefundOrderListVC ()<YQRefundGoodInfoCellDelegate>

@property (strong, nonatomic) YQRefundOrderModel *model;

@end

@implementation YQRefundOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"退款/售后");
}

-(void)setupView {
    [super setupView];

    kYQREGISTERTABLEVIEWCELL(YQRefundGoodInfoCell)
    self.listTableView.separatorColor = HexColor(0xCDCDCD);
    [self setupRefresh:self.listTableView];
    [self onStartRefresh];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getRefundOrderList];
}

-(void)onStartRefresh {
    self.page = 1;
    [self getRefundOrderList];
}

- (void)onStartLoadMore {
    self.page ++ ;
    [self getRefundOrderList];
}

-(void)getRefundOrderList {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"limit"] = @"10";
    parameters[@"page"] = @(self.page);
    
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendPostRequest:kPOST_REFUND_LIST_URL parameters:parameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        if (responseObject.code == 0) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            NSArray *arr = [NSArray modelArrayWithClass:YQRefundOrderModel.class json:responseObject.data[@"list"]];
            [self.dataArr addObjectsFromArray:arr];
            [self.listTableView reloadData];
            
            if (arr.count < 10) {
                [self.listTableView.mj_footer endRefreshingWithNoMoreData];
            }
            
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error);
        [self stopRefresh];
    }];
    
}


#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQRefundOrderModel *refundModel = self.dataArr[indexPath.row];
//售后商品属性
    YQRefundGoodInfoCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:@"YQRefundGoodInfoCell"];
    goodsCell.refundModel = refundModel;
    goodsCell.delegate = self;
    return goodsCell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 200;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    YQRefundOrderModel *ordermOrderModel = self.dataArr[indexPath.row];
    
    YQRefundOrderDetailsVC *detailsVC = [YQRefundOrderDetailsVC new];
    detailsVC.orderModel = ordermOrderModel;
    kYQPUSH(detailsVC);
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end


#pragma mark - YQRefundGoodInfoCellDelegate 售后按钮点击事件
//删除订单
- (void)selectDeleteBtnWithModel:(YQRefundOrderModel *)model
{
    
}

-(void)selectDetailBtnWithModel:(YQRefundOrderModel *)model {
        
    YQRefundOrderDetailsVC *detailsVC = [YQRefundOrderDetailsVC new];
    detailsVC.orderModel = model;
    kYQPUSH(detailsVC);

}

#pragma mark - 撤销申请
-(void)refundCancelOrderModel:(YQGoodsOrderItemModel *)orderModel {
    
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendGetRequest:kGET_REFUND_CANCEL_URL(orderModel.orderItemId) parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        YQUITIPSSUCCEED(responseObject.msg)
        if (responseObject.code == 0) {
            [self getRefundOrderList];
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQUITIPSERROR(error)
        YQHIDELOADINGTIPS
    }];
}


@end
